package com.源码分析.以编程方式创建流;

import java.util.function.Consumer;

import reactor.core.publisher.Mono;

public class CreateMonoSink {

	public static void main(String[] args) {
		Client client = new Client();
		Mono.<String>create(sink -> {
			HttpListener<Event> listener = event -> {
				if (event.getResponseCode() >= 400) {
					sink.error(new RuntimeException("Failed"));
				} else {
					String body = event.getBody();
					if (body.isEmpty()) {
						sink.success();
					} else {
						sink.success(body.toLowerCase());
					}
				}
			};

			client.addListener(listener);

			sink.onDispose(() -> client.removeListener(listener));
		});

	}

	private static interface HttpListener<T> extends Consumer<T> {

	}

	private static class Event {

		public int getResponseCode() {
			return 0;
		}

		public String getBody() {
			return null;
		}

	}

	private static class Client {

		public void addListener(HttpListener<Event> listener) {

		}

		public Object removeListener(HttpListener<Event> listener) {
			return null;
		}

	}

}
